//dropdown auto right			 
 $('.file-item .dropdown a').click(function(){
 	var outWidth =$('.file-list').width();
	var div = $(this).closest('.file-item');
	var pos =div.position().left;
	var posR = outWidth-pos;
	if(posR<200){
		$(this).next('.dropdown-menu').addClass('r');					
	}			
 })

//file-item
//文件操作-删除
$('.file-list').on('click','.btn_file_del',function(){
	$(this).closest('.file-item').remove();
})
//文件操作-重命名
$('.file-list').on('click','.btn_file_rename',function(){	
	var val = $(this).closest('.file-item').find('.file-item-name').text();	
	$(this).closest('.file-item').find('.file-item-name').hide().next('.renameplace').show();
	$(this).closest('.file-item').find('.file-item-rename').val(val).select();
})
$('.file-list').on('click','.file-rename-confirm',function(){
	var val = $(this).siblings('input').val();			
	if(val==''){
		alert('文件(夹)名，不能为空哟')
	}else{
		$(this).closest('.file-item').find('.file-item-name').text(val).show();			
		$(this).parent('.renameplace').hide();
	}
	
	$('.btn-folder-create').one('click',createFolder);
})
$('.file-list').on('click','.file-rename-cancel',function(){			
	$(this).closest('.file-item').find('.file-item-name').show();			
	$(this).parent('.renameplace').hide();	
	
	$('.btn-folder-create').one('click',createFolder);
})

//文件操作-新建文件夹		
function createFolder(){
	var d = new Date();
	var now = d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate()+" "+d.getHours()+":"+d.getMinutes(); 
	var fileItem = "<div class=\"file-item\">"+
					"<div class=\"dropdown text-right\">"+
					"<a href=\"javascript:;\" data-toggle=\"dropdown\" aria-expanded=\"false\"><i class=\"iconfont icon-more\"></i></a>"+
					"<ul class=\"dropdown-menu\">"+
					"<li><a href=\"javascript:;\" class=\"btn_file_open\">打开</a></li>"+
					"<li><a href=\"javascript:;\" class=\"btn_file_download\">下载</a></li>"+
					"<li><a href=\"javascript:;\" class=\"btn_file_del\">删除</a></li>"+
					"<li><a href=\"javascript:;\" class=\"btn_file_rename\">重命名</a></li>"+
					"<li><a href=\"javascript:;\" class=\"btn_file_move\">移动到</a></li>"+
					"</ul>"+
					"</div>"+									
					"<p class=\"text-center file-item-thumb\"><img src=\"images/file_thumb_folder.png\"></p>"+
					"<p class=\"text-center\">"+
					"<span class=\"dib file-item-name\" style=\"display: none;\">新建文件夹</span>"+
					"<span class=\"renameplace\" style=\"display: block;\">"+
					"<input type=\"text\" value=\"新建文件夹\" class=\"cursor blink file-item-rename\" />"+
					"  <i class=\"gicon gicon-ok file-rename-confirm\"></i>"+
					"  <i class=\"gicon gicon-remove file-rename-cancel\"></i>"+
					"</span>"+							
					"</p>"+
					"<div class=\"row file-item-des\">"+
					"<div class=\"col-sm-4\">"+											
					"新建文件夹</div>"+
					"<div class=\"col-sm-4 text-center o-3\">"+										
					"文件夹</div>"+
					"<div class=\"col-sm-4 text-right o-3\">"+										
					"修改时间："+now+"</div>"+
					"</div>"+
					"</div>";
					
					
	$(".file-tab-contents .active .file-list").prepend(fileItem);
	$('.file-tab-contents .active .file-list .file-item').eq(0).find('.file-item-rename').focus().select();
}

$('.btn-folder-create').on('click',function(event){
	createFolder();			
	$(this).unbind('click');
});
//文件操作-移动到
$('.file-list').on('click','.btn_file_move',function(){
	$('.popup-moveto').show();
})
//文件介绍侧边栏
$('.file-list').on('click','.fileintro',function(){	
	$('#fileIntroRight').toggleClass("na-menu-open");
	$('body').addClass('noscroll');	
	$('.file-mainarea').toggleClass('hassider');
})
//文件操作提示
$(function () {
  $('[data-toggle="tooltip"]').tooltip()
})  
		
//tree
$('.tree li:has(ul)').addClass('parent_li');
 $('.tree li > span').on('click', function (e) {   		 	
 	$('.tree li span').removeClass('active');
 	$(this).addClass('active');
 	
	var children = $(this).parent('li.parent_li').find(' > ul > li');
	if (children.is(":visible")) {
   	 	children.hide();
    	$(this).find('i').addClass('gicon-plus-sign').removeClass('gicon-minus-sign');
    	$(this).find('b').addClass('gicon-folder-close').removeClass('gicon-folder-open');
        } else {		
            children.show();		
            $(this).find('i').addClass('gicon-minus-sign').removeClass('gicon-plus-sign');		
            $(this).find('b').addClass('gicon-folder-open').removeClass('gicon-folder-close');		
        }		
        e.stopPropagation();		
  });
//侧边栏tab高度  及file-list高度
function namenuTabHeight(){
	var winH,eleH1,eleH2,eleH3,tabH;
		winH = $(window).height();
		eleH1 = $('.na-menu-title').outerHeight();
		eleH2 = $('.file-operation').outerHeight();
		eleH3 = $('.file-relate').outerHeight();
		
		tabH = (winH-eleH1-eleH2-eleH3-30-50); //50 topnav  50 tabTitle 70 dialog
		flH = (winH-280);
		
	$('.sidebarR-tab .tab-content').css({'height':tabH+'px'})
	$('.file-list').css({'overflow-y':'auto','height':flH+'px'});	
}
$(function(){
    namenuTabHeight();
    $(window).bind('resize',namenuTabHeight);		    
});

//图片放大带介绍栏
$(".imgclass2").click(function(){
	var thiselement=$(this);
	imgShow2("#outdiv","#indiv","#bigimg",thiselement);			
});

function imgShow2(outdiv,indiv,bigimg,_this){
    var src = _this.attr("src");
    $(bigimg).attr("src", src);	       
    $("<img/>").attr("src", src).load(function(){

		var windowW = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
		var windowH = window.innerWideth || document.documentElement.clientHeight || document.body.clientHeight;
		
        var realWidth = this.width;
        var realHeight = this.height;
        var imgWidth, imgHeight;
        var scale = 0.8;
         
       if(realWidth>800){
       	imgWidth = 800;
       	imgHeight = imgWidth/realWidth*realHeight
       }else{
       	imgWidth = realWidth;
       	imgHeight = realHeight;
       }

      	$(bigimg).css("width",imgWidth);
        var w = (windowW-400-imgWidth)/2;
        var h = (windowH-100)/2;
        var h2 = windowH - 100;
        var pt;
        
        if(w<0){
        	w=0
        }
        
        if(realHeight>h2){        	
        	pt = 0
        }else{
        	pt = (h2-realHeight)/2;
        }
        
       
        $(indiv).css({"left":w,'top':'50px','height':h2,"overflow":'hidden','padding-top':pt});       
        $(indiv).find('.ps-scrollbar-y-rail').remove();
        $(indiv).find('.ps-scrollbar-x-rail').remove();
        

        $(outdiv).fadeIn("fast");
    });     
    $('.gicon-guanbi').click(function(){
        $('#outdiv').fadeOut("fast");
        $('#fileIntroRight').removeClass('na-menu-open');
        $('.file-mainarea').removeClass('hassider');
        $('body').removeClass('noscroll');
        $('#indiv .myscroll').perfectScrollbar('destroy')
    });
}



//文件操作 编辑-分享-删除等
$('body').on('click','.btn-edit-modal',function(){
	$('.del-modal').hide();
	$('.share-modal').hide();
	$('.edit-modal').show();
});
$('body').on('click','.btn-del-modal',function(){	
	$('.del-modal').show();
	$('.share-modal').hide();
	$('.edit-modal').hide();
});
$('body').on('click','.btn-share-modal',function(){	
	$('.del-modal').hide();
	$('.share-modal').show();
	$('.edit-modal').hide();
});

$('.list-group-sidebar').on('click','.list-group-item',function(){
	$(this).addClass('active').siblings('.list-group-item').removeClass('active');
})
function listgroupsidebarH(){
	var eleH1,eleH2,eleH3,eleH4,eleH5,targetH;
	eleH1 = $('.sidebar').height();	
	targetH = eleH1-24;
	$('.list-group-sidebar').css({'height':targetH+'px'});	
}
function guanbi(){
	var w = $(window).width();
	var r = w*0.21;

	if(r<400){
		$('.gicon-guanbi').css({'right':'420px'})
	}
}

function indiv(){
	var win,w,div;
	win = $(window).width();
	div = $('#indiv').width();
	w = (win-div-400)/2;
	
	$('#indiv').css({'left':w})
}

$(function(){
    listgroupsidebarH();
    guanbi();
    $(window).on('resize',function(){
    	listgroupsidebarH();
    	guanbi();
    	indiv();
    });
});